import { getRoleListAPI, deleteRoleAPI, getCompanyInfoAPI, addRoleAPI, findRoleAPI, updateRoleAPI,assignPremAPI } from '@/api/settingsApi'

const actions = {
  /* 发送请求获取角色列表的action */
  async fetchRoleList(context, pageParams) {
    const { data: roleInfo } = await getRoleListAPI(pageParams)
    context.commit('setRoleList', roleInfo)
  },
  /* 发送请求删除角色的action */
  async deleteRole(context, id) {
    const { message } = await deleteRoleAPI(id)
    return message
  },
  /* 发送请求添加角色的action */
  async addRole(context, data) {
    const { message } = await addRoleAPI(data)
    return message
  },
  /* 发送请求获取角色详情的接口 */
  async findRole(context, id) {
    const { data: role } = await findRoleAPI(id)
    return role
  },
  /* 发送请求修改角色详情的接口 */
  async updateRole(context, data) {
    const { message } = await updateRoleAPI(data)
    return message
  },
  /* 发送请求获取企业信息的action */
  async fetchCompanyInfo(context, id) {
    const { data: companyInfo } = await getCompanyInfoAPI(id)
    context.commit('setCompanyInfo', companyInfo)
  },
  /* 发送请求给角色分配权限的action */
  async assignPrem({ commit }, params) {
    const { message } = await assignPremAPI(params)
    return message
  }
}
const mutations = {
  setRoleList(state, roleInfo) {
    state.roleInfo = roleInfo
  },
  setCompanyInfo(state, companyInfo) {
    state.companyInfo = companyInfo
  }
}
const state = () => {
  return {
    /* 角色信息 */
    roleInfo: {},
    /* 企业信息 */
    companyInfo: {}
  }
}
const getters = {
  /* 角色列表 */
  roleList: state => state.roleInfo.rows,
  /* 数据总条数 */
  total: state => state.roleInfo.total
}
export default {
  actions,
  mutations,
  state,
  getters,
  namespaced: true
}
